<template>
<h1>稿件管理页面</h1>
  <el-radio-group v-model="type" @change="loadContents()">
    <el-radio-button label="1">烘焙食谱</el-radio-button>
    <el-radio-button label="2">烘焙视频</el-radio-button>
    <el-radio-button label="3">行业资讯</el-radio-button>
  </el-radio-group>
  <el-button style="position: relative;left:20px;top: 3px"
             type="primary" @click="router.push('/personal/post')">发布内容</el-button>
  <el-table :data="arr">
    <el-table-column type="index" label="编号" width="100px"></el-table-column>
    <el-table-column prop="title" label="标题" width="200px"></el-table-column>
    <el-table-column label="封面" width="100px">
      <template #default="scope">
        <img :src="'http://localhost:8080'+scope.row.imgUrl" style="width: 100%">
      </template>
    </el-table-column>
    <el-table-column prop="categoryName" label="分类" ></el-table-column>
    <el-table-column prop="viewCount" label="浏览量"></el-table-column>
    <el-table-column prop="commentCount" label="评论量" ></el-table-column>
    <el-table-column prop="createTime" label="发布时间" width="180px"></el-table-column>
    <el-table-column label="操作" width="200px" fixed="right">
      <template #default="scope">
        <el-button type="success" @click="update(scope.row)">编辑</el-button>
        <el-button type="danger" @click="del(scope.$index,scope.row)">删除</el-button>
      </template>
    </el-table-column>
  </el-table>
</template>

<script setup>
import router from "@/router";
import {onMounted, ref} from "vue";
import axios from "axios";
import qs from "qs";
import {ElMessage} from "element-plus";
const update = (content)=>{
  router.push('/personal/post?id='+content.id);
}
const loadContents = ()=>{
  let user = localStorage.user?JSON.parse(localStorage.user):null;
  let data = qs.stringify({userId:user.id,type:type.value});
  //请求当前用户发布的所有内容
  axios.get('http://localhost:8080/v1/contents/management?'+data)
      .then((response)=>{
        if (response.data.code==2001){
          arr.value = response.data.data;
        }
      })
}
const del = (i,content)=>{
  if (confirm("您确认删除此内容吗?")){
    axios.post('http://localhost:8080/v1/contents/'+content.id+"/delete")
    .then((response)=>{
      if (response.data.code==2001){
        ElMessage.success("删除完成!");
        arr.value.splice(i,1);//从页面中删除
      }
    })
  }
}
const type = ref(1);
const arr = ref([]);
onMounted(()=>{
  loadContents();
})
</script>

<style scoped>

</style>